﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Data.Linq.SqlClient;
using System.Text;
using System.Data;
using Rmlife.Data;
using Rmlife.Common;
using Rmlife.Config;
using Rmlife.Model.Global;
using System.Collections;
using Rmlife.Model;

namespace Rmlife.Site
{
    public class Cosmes
    {
        private static readonly string configPath = Utils.GetMapPath(BaseConfigs.GetSitePath.ToLower() + "xml/navmenu.config");

        public static DataSet GetXml()
        {
            string jsPath = Utils.GetMapPath(BaseConfigs.GetSitePath.ToLower() + "xml/navmenu.js");
            System.Data.DataSet dsSrc = new System.Data.DataSet();
            dsSrc.ReadXml(configPath);
            return dsSrc;
        }
        /// <summary>
        /// 头部导航
        /// </summary>
        /// <param name="strwhere"></param>
        /// <returns></returns>
        public static List<XmlConfigInfo.ToptabmenuInfo> GetTopList(string strwhere)
        {
            DataSet dsSrc = GetXml();
            var dt = dsSrc.Tables[2].AsEnumerable();
            var qt = from dr in dt
                     where dr.Field<string>("title").Contains(strwhere)
                     select new XmlConfigInfo.ToptabmenuInfo
                     {
                         id = dr[0] == null ? 0 : Convert.ToInt32(dr[0]),
                         title = dr.Field<string>("title"),
                         mainmenulist = dr.Field<string>("mainmenulist"),
                         mainmenuidlist = dr.Field<string>("mainmenuidlist"),
                         defaulturl = dr.Field<string>("defaulturl")
                     };
            return qt.ToList();
        }

        /// <summary>
        /// 根部导航
        /// </summary>
        /// <param name="topMenuList">toptabmenu => mainmenuidlist</param>
        /// <param name="strwhere">menutitle</param>
        /// <returns></returns>
        public static List<XmlConfigInfo.MainmenuInfo> GetMainMenuList(string[] topMenuList, string strwhere)
        {
            DataSet dsSrc = GetXml();
            var dt = dsSrc.Tables[0].AsEnumerable();
            var qt = from dr in dt
                     where topMenuList.Contains(dr.Field<string>("menuid")) && dr.Field<string>("menutitle").Contains(strwhere)
                     select new XmlConfigInfo.MainmenuInfo
                     {
                         id = Convert.ToInt32(dr["id"].ToString()),
                         menuid = Convert.ToInt32(dr["menuid"].ToString()),
                         menutitle = dr.Field<string>("menutitle")
                     };
            return qt.ToList();
        }
        /// <summary>
        /// 左侧子菜单
        /// </summary>
        /// <param name="mainMenuList"></param>
        /// <param name="strwhere"></param>
        /// <returns></returns>
        public static List<XmlConfigInfo.SubmainInfo> GetSubMenuList(string mainMenuId, string strwhere)
        {
            DataSet dsSrc = GetXml();
            var dt = dsSrc.Tables[1].AsEnumerable();
            var qt = from dr in dt
                     where dr.Field<string>("menuparentid") == mainMenuId && dr.Field<string>("menutitle").Contains(strwhere)
                     select new XmlConfigInfo.SubmainInfo
                     {
                         menuparentid = Convert.ToInt32(dr["menuparentid"]),
                         menutitle = dr.Field<string>("menutitle"),
                         link = dr.Field<string>("link"),
                         frameid = dr.Field<string>("frameid")
                     };
            return qt.ToList<XmlConfigInfo.SubmainInfo>();
        }
        /// <summary>
        /// 获得所有XML：用于构造树形结构
        /// </summary>
        /// <returns></returns>
        public static List<QueryTreeInfo> GetAllMenuList()
        {
            List<QueryTreeInfo> treeList = new List<QueryTreeInfo>();
            DataSet dsSrc = GetXml();
            var dtTop = dsSrc.Tables[2].AsEnumerable();
            int i = 1;
            foreach (var item in dtTop)
            {
                QueryTreeInfo infoTree = new QueryTreeInfo();
                infoTree.id = i;
                infoTree.pId = 0;
                infoTree.name = item.Field<string>("title");
                infoTree.menuUrl = item.Field<string>("defaulturl");
                treeList.Add(infoTree);
                string[] topMenuList = item.Field<string>("mainmenuidlist").Split(',');
                List<XmlConfigInfo.MainmenuInfo> listMain = GetMainMenuList(topMenuList, "");
                foreach (XmlConfigInfo.MainmenuInfo itemM in listMain)
                {
                    QueryTreeInfo infoTreeM = new QueryTreeInfo();
                    i++;
                    infoTreeM.id = i;
                    infoTreeM.pId = infoTree.id;
                    infoTreeM.name = itemM.menutitle;
                    infoTreeM.menuUrl = infoTree.menuUrl;
                    treeList.Add(infoTreeM);
                    List<XmlConfigInfo.SubmainInfo> listSub = GetSubMenuList(itemM.menuid.ToString(), "");
                    foreach (XmlConfigInfo.SubmainInfo itemS in listSub)
                    {
                        QueryTreeInfo infoTreeS = new QueryTreeInfo();
                        i++;
                        infoTreeS.id = i;
                        infoTreeS.pId = infoTreeM.id;
                        infoTreeS.name = itemS.menutitle;
                        infoTreeS.menuUrl = infoTreeM.menuUrl;
                        treeList.Add(infoTreeS);
                    }
                }
                i++;
            }
            return treeList;
        }
    }
}
